package com.seelyn.ddvideo.web;

import com.github.sd4324530.fastweixin.api.MenuAPI;
import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.entity.Menu;
import com.github.sd4324530.fastweixin.api.entity.MenuButton;
import com.github.sd4324530.fastweixin.api.enums.MenuType;
import com.github.sd4324530.fastweixin.handle.EventHandle;
import com.github.sd4324530.fastweixin.handle.MessageHandle;
import com.github.sd4324530.fastweixin.message.Article;
import com.github.sd4324530.fastweixin.message.BaseMsg;
import com.github.sd4324530.fastweixin.message.NewsMsg;
import com.github.sd4324530.fastweixin.message.req.BaseEvent;
import com.github.sd4324530.fastweixin.servlet.WeixinControllerSupport;
import com.seelyn.ddvideo.model.WeiXinMenu;
import com.seelyn.ddvideo.service.WeiXinMenuService;
import com.seelyn.ddvideo.weixin.handler.SearchVideoMessageHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/20
 */
@RestController
@RequestMapping("/weixin")
public class WeixinController extends WeixinControllerSupport {

    private static final Logger log = LoggerFactory.getLogger(WeixinController.class);
    private static final String TOKEN = "seelyn_weixin";
    private static final String siteUrl = "http://www.seelyn.com";

    @Autowired
    private SearchVideoMessageHandle searchVideoMessageHandle;
    @Autowired
    private WeiXinMenuService weiXinMenuService;

    //设置TOKEN，用于绑定微信服务器
    @Override
    protected String getToken() {
        return TOKEN;
    }

    //使用安全模式时设置：APPID
    //不再强制重写，有加密需要时自行重写该方法
    @Override
    protected String getAppId() {
        return "wxe79d0fa91849f981";
    }

    //使用安全模式时设置：密钥
    //不再强制重写，有加密需要时自行重写该方法
    @Override
    protected String getAESKey() {
        return "";
    }

    private String getAppSecret() {
        return "d0999a76c2eb2e91859b71ae98c5c688";
    }

//    //重写父类方法，处理对应的微信消息
//    @Override
//    protected BaseMsg handleTextMsg(TextReqMsg msg) {
//        String content = msg.getContent();
//        log.debug("用户发送到服务器的内容:{}", content);
//        return new TextMsg("服务器回复用户消息!");
//    }

    @PostConstruct
    public void createMenu() {
        ApiConfig config = new ApiConfig(getAppId(), getAppSecret(), true);
        MenuAPI menuAPI = new MenuAPI(config);

        WeiXinMenu weiXinMenu = weiXinMenuService.findByMenuId("menu_001");
        if (weiXinMenu == null) {
            weiXinMenu = new WeiXinMenu();
            weiXinMenu.setMenuId("menu_001");

            List<MenuButton> menuButtons = new ArrayList<>();
            MenuButton piankuButton = new MenuButton();
            piankuButton.setName("影片库");
            piankuButton.setType(MenuType.CLICK);
            piankuButton.setKey("help_operation");

            List<MenuButton> subMenuButtons = new ArrayList<>();
            MenuButton subBtn1 = new MenuButton();
            subBtn1.setName("电影");
            subBtn1.setType(MenuType.VIEW);
            subBtn1.setUrl("http://www.seelyn.com/#/movie");
            subMenuButtons.add(subBtn1);

            MenuButton subBtn2 = new MenuButton();
            subBtn2.setName("电视剧");
            subBtn2.setType(MenuType.VIEW);
            subBtn2.setUrl("http://www.seelyn.com/#/tv");
            subMenuButtons.add(subBtn2);

            MenuButton subBtn3 = new MenuButton();
            subBtn3.setName("动漫");
            subBtn3.setType(MenuType.VIEW);
            subBtn3.setUrl("http://www.seelyn.com/#/cartoon");
            subMenuButtons.add(subBtn3);

            piankuButton.setSubButton(subMenuButtons);
            menuButtons.add(piankuButton);

            MenuButton siteButton = new MenuButton();
            siteButton.setName("凌晨电影");
            siteButton.setType(MenuType.VIEW);
            siteButton.setUrl("http://www.seelyn.com");
            menuButtons.add(siteButton);

            MenuButton aboutButton = new MenuButton();
            aboutButton.setName("关于作者");
            aboutButton.setType(MenuType.VIEW);
            aboutButton.setUrl("http://www.seelyn.com/#/about");
            menuButtons.add(aboutButton);

            weiXinMenu.setButton(menuButtons);
            menuAPI.createMenu(weiXinMenu);
            weiXinMenuService.saveMenu(weiXinMenu);
        }


    }

    /*1.1版本新增，重写父类方法，加入自定义微信消息处理器
     *不是必须的，上面的方法是统一处理所有的文本消息，如果业务觉复杂，上面的会显得比较乱
     *这个机制就是为了应对这种情况，每个MessageHandle就是一个业务，只处理指定的那部分消息
     */
    @Override
    protected List<MessageHandle> initMessageHandles() {
        List<MessageHandle> handles = new ArrayList<MessageHandle>();
        handles.add(searchVideoMessageHandle);
        return handles;
    }

    //1.1版本新增，重写父类方法，加入自定义微信事件处理器，同上
    @Override
    protected List<EventHandle> initEventHandles() {
        List<EventHandle> handles = new ArrayList<EventHandle>();
//        handles.add(new MyEventHandle());
        return handles;
    }

    @Override
    protected BaseMsg handleSubscribe(BaseEvent event) {

        Article article = new Article("凌晨电影网", siteUrl);
        article.setDescription("更多更好看的视频节目，尽在凌晨电影网,想看某某电影的话，直接回复[某某电影]名称就可以了");
        NewsMsg indexMsg = new NewsMsg(1);
        indexMsg.add(article);
        return indexMsg;
    }
}
